package com.santana.parser;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import com.santana.parser.googleMovie.HTMLParser;

public class DirectoryReader {

	private Properties properties = new Properties();		
	private static Logger log = Logger.getLogger(DirectoryReader.class);

	public static IndexWriter w = null;

	public Directory getDocuments() {

		Directory index = new RAMDirectory();
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44,
				Lucene.analyzer);

		try {

			w = new IndexWriter(index, config);

			properties.load(new FileInputStream("config.properties"));
			String [] cities = properties.getProperty("cities").split(",");
			

			for(String city:cities){

				URL url = new URL("http://www.google.com/movies?near="
						+ URLEncoder.encode(city, "UTF-8"));
				url.openConnection();

				HTMLParser parser = new HTMLParser();
				List<String> movies = parser.getElements(url, null, null);

				for (String movie : movies) {
					this.addHTML(w, city, movie);
				}
			}

			w.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return index;
	}

	private void addHTML(IndexWriter w, String city, String movie)
			throws IOException {

		Document doc = new Document();

		doc.add(new TextField("city", city, Field.Store.YES));
		doc.add(new TextField("movie", movie, Field.Store.YES));

		w.addDocument(doc);
		System.out.println("- " + city + " - " + movie);
	}
}